package org.mineground.commands.player.admin;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.mineground.Main;
import org.mineground.PermissionNodes;
import org.mineground.Utilities;

public class MTP implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) throws CommandException {
	Player player = (Player) sender;

	if (!player.hasPermission(PermissionNodes.PERMISSION_MTP_COMMAND)) {
            return true;
        }
        
	if (args.length == 0) {
	    player.sendMessage(ChatColor.RED + "* Usage: /mtp <player>");
	    return true;
	}

	Player recv = Main.getInstance().getServer().getPlayer(args[0]);

	if (recv == null) {
	    player.sendMessage(ChatColor.RED + "* Invalid player.");
	    return true;
	}

	if (player.equals(recv)) {
	    player.sendMessage(ChatColor.RED + "* Error: There is no need to secretly teleport to yourself, silly!");
	    return true;
	}
   
  if (recv.getWorld().equals(Main.getInstance().getServer().getWorld(Main.getInstance().getConfigHandler().getSurvivalWorldName()))) {
      player.sendMessage(ChatColor.RED + "* Error: That player is in the survival world right now and you are not allowed to secretly teleport to him/her.");
      return true;
  }

	else {
	    Main.getInstance().getIRCHandler().sendMessage("@" + Main.getInstance().getConfigHandler().getIrcChannel(), Colors.BROWN + "* " + Utilities.fixName(player) + Colors.BROWN + " has been secretly teleported to " + Utilities.fixName(recv) + Colors.BROWN + ".");
	    player.teleport(recv);
	    player.sendMessage(ChatColor.DARK_GREEN + "*  You have been secretly teleported to " + recv.getDisplayName() + ".");
	}
        
	return true;
    }
}
